package com.example.book.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

@TableName("user")
@Data
public class User {
    @TableId (type = IdType.AUTO)
    private Integer id;
    public String username;
    private String nickName;
    private String password;
    private String sex;
    private String address;
    private String phone;
    @TableField(exist = false)  //表中没有token不会报错仍能编译运行
    private String token;
    private Integer role;
    @TableField(exist = false)
    private String code;
    @TableField(exist = false)
    private String confirm;
    private String alow;   //是否允许借阅的指标

    public List<Book> bookList = new ArrayList<>();
    public User() {}

    public User(String username) {
        this.username = username;
    }

    public User set(String name, int score) {//设置书名和评分
        this.bookList.add(new Book(name,score));
        return this;
    }

    public Book find(String name) {
        for(Book book : bookList) {//根据书名查找用户
            if(book.name.equals(username)) {
                return book;
            }
        }
        return null;
    }

    @Override
    public String toString() {
        return "User{" + "username:'" + username + '\''+ '}';
    }
}
